package com.yun.datastructure.tree.binarysearchtree.leetcode;

import com.yun.datastructure.tree.binarytree.domain.TreeNode;

public class Leetcode701 {
    public TreeNode insertIntoBST(TreeNode root, int val) {
        TreeNode node=root;
        TreeNode parent=null;
        while (node!=null){
            parent=node;
            if(val<node.val){
                node=node.left;
            }else{
                node=node.right;
            }
        }
        if(parent==null){
            root=new TreeNode(val);
            return root;
        }
        if(val< parent.val){
            parent.left=new TreeNode(val);
        }else {
            parent.right=new TreeNode(val);
        }
        return root;
    }
}
